#!/usr/bin/python
# -*- coding: UTF-8 -*-

from Tools.Logs import Logs
import os

class HDFS :

    def contrast(self ,file1 ,file2):
        src_dic = {}
        desc_dir = {}
        for i in Logs(file1).read():
            dir = i.split("\t")[0]
            dirsize = i.split("\t")[-1].split("\n")[0]
            src_dic[dir] = dirsize
        for i in Logs(file2).read():
            dir = i.split("\t")[0]
            dirsize = i.split("\t")[-1].split("\n")[0]
            desc_dir[dir] = dirsize
        for k, v in desc_dir.items():
            if src_dic.get(k) != v:
                print "dir : %s \t src size : %s \t des size : %s " % (k , src_dic.get(k),desc_dir.get(k))
                Logs("redistcp.log").add( k +"\n")


    def redistcp(self ,cluster1,cluster2,filename):
        distcp_tmp_cmd = "hdfs dfs -rm -r %s/.distcp.tmp.attempt*" %(filename)
        distcp_cmd = "hadoop distcp -m 200 -skipcrccheck -update -bandwidth 1500 %s%s %s%s" % \
        (cluster1, filename, cluster2, filename)
        dirls = "hdfs dfs -ls %s " % (filename)
        os.system(distcp_cmd)
